﻿using System;
using System.Collections.Specialized;
using System.Net;

namespace FBGraph
{
    /// <summary>Defines a contract for <see cref="IFBGraphController" /> implementations that provide
    /// access to Graph API search functionality.</summary>
    /// <typeparam name="TModel">The type of item that is returned from the search.</typeparam>
    public interface IFBGraphSearchController<TModel> : IFBGraphController, IFBGraphPrimaryController<TModel>
        where TModel : class, IFBGraphResult, new()
    {
        /// <summary>Gets the Graph API item type.</summary>
        String SearchType { get; }
    }

    /// <summary>Contains extension methods for types implementing <see cref="IFBGraphSearchController{TModel}" />.</summary>
    public static class FBGraphSearchControllerExtensions
    {
        /// <summary>Performs a Graph API search for the given query and constraining parameters.</summary>
        /// <typeparam name="TModel">The type of object to return.</typeparam>
        /// <param name="controller">The controller.</param>
        /// <param name="query">The search term or keywords.</param>
        /// <returns>A <see cref="PagedResult{TModel}" /> containing the results of the search query.</returns>
        public static PagedResult<TModel> Search<TModel>(this IFBGraphSearchController<TModel> controller, String query)
            where TModel : class, IFBGraphResult, new()
        {
            return controller.Search<TModel>(query, null, null, null, null);
        }

        /// <summary>Performs a Graph API search for the given query and constraining parameters.</summary>
        /// <typeparam name="TModel">The type of object to return.</typeparam>
        /// <param name="controller">The controller.</param>
        /// <param name="query">The search term or keywords.</param>
        /// <param name="since">The minimum post date/time to filter results by.</param>
        /// <param name="until">The maximum post date/time to filter results by.</param>
        /// <param name="limit">The maximum number of objects to return.</param>
        /// <param name="offset">The index offset from which to start returning results.</param>
        /// <returns>A <see cref="PagedResult{TModel}" /> containing the results of the search query.</returns>
        public static PagedResult<TModel> Search<TModel>(this IFBGraphSearchController<TModel> controller, String query, DateTime? since, DateTime? until, Int32? limit, Int32? offset)
            where TModel : class, IFBGraphResult, new()
        {
            var result = controller.Context.Search<TModel>(query, controller.SearchType, controller.GetFields(), since, until, limit, offset);
            return result;
        }
    }
}